package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SySelfServiceBookingOrderLog;
import com.ruoyi.system.dto.SySelfBookingRefundOrderLogDTO;
import com.ruoyi.system.enumeration.SySelfServiceBookingOrderLogEnum;
import com.ruoyi.system.mapper.SySelfServiceBookingOrderLogMapper;
import com.ruoyi.system.service.SySelfServiceBookingOrderLogService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.StoreIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SySelfServiceBookingOrderLogServiceImpl extends ServiceImpl<SySelfServiceBookingOrderLogMapper, SySelfServiceBookingOrderLog> implements SySelfServiceBookingOrderLogService {

    @Autowired
    private StoreIdUtils storeIdUtils;

    @Override
    public IPage<SySelfServiceBookingOrderLog> all(SySelfBookingRefundOrderLogDTO logDTO) {
        LambdaQueryWrapper<SySelfServiceBookingOrderLog> wrapper = Wrappers.lambdaQuery();
        if (!AbdulEmptyUtils.isEmpty(logDTO.getVipMessage())) {
            wrapper.eq(SySelfServiceBookingOrderLog::getVipMessage, logDTO.getVipMessage());
        }
        if (!AbdulEmptyUtils.isEmpty(logDTO.getBookingNumber())) {
            wrapper.eq(SySelfServiceBookingOrderLog::getBookingNumber, logDTO.getBookingNumber());
        }
        if (!AbdulEmptyUtils.isEmpty(logDTO.getType())) {
            wrapper.eq(SySelfServiceBookingOrderLog::getType, logDTO.getType());
        }
        if (!AbdulEmptyUtils.isEmpty(logDTO.getDesk())) {
            wrapper.eq(SySelfServiceBookingOrderLog::getDesk, logDTO.getDesk());
        }
        if (!AbdulEmptyUtils.isEmpty(logDTO.getTableClassification())) {
            wrapper.eq(SySelfServiceBookingOrderLog::getTableClassification, logDTO.getTableClassification());
        }
        if (!AbdulEmptyUtils.isEmpty(logDTO.getStartTime()) && !AbdulEmptyUtils.isEmpty(logDTO.getEndTime())) {
            wrapper.between(SySelfServiceBookingOrderLog::getCreateTime, logDTO.getStartTime(), logDTO.getEndTime());
        }
        if (!AbdulEmptyUtils.isEmpty(logDTO.getOperator())) {
            wrapper.eq(SySelfServiceBookingOrderLog::getCreateBy, logDTO.getOperator());
        }
        wrapper.eq(SySelfServiceBookingOrderLog::getStoreId,storeIdUtils.getStoreId());
        wrapper.orderByDesc(SySelfServiceBookingOrderLog::getUpdateTime);
//        List<SySelfServiceBookingOrderLog> list = this.list(wrapper);
        Page<SySelfServiceBookingOrderLog> page = new Page<>(logDTO.getPageNum(), logDTO.getPageSize());
        Page<SySelfServiceBookingOrderLog> logPage = this.page(page, wrapper);
        if (!AbdulEmptyUtils.isEmpty(logPage.getRecords())) {
            logPage.getRecords().forEach(item -> {
                Integer type = item.getType();
                if (!AbdulEmptyUtils.isEmpty(type)) {
                    item.setTypeDesc(SySelfServiceBookingOrderLogEnum.getDescriptionByCode(type));
                }
            });
        }
        return page;
    }

    @Override
    public boolean add(SySelfServiceBookingOrderLog log) {
        log.setCreateTime(new Date());
        log.setUpdateTime(new Date());
        log.setCreateBy(SecurityUtils.getUsername());
        log.setUpdateBy(SecurityUtils.getUsername());
        log.setStoreId(storeIdUtils.getStoreId());
        return this.save(log);
    }
}
